目標:能安全地開分支、切換、合併,並處理衝突。
git branch feature/login 建立分支
git switch feature/login 切換(新指令推薦)
git switch -c feature/signup 同時建立+切換
git branch -v 查看分支
未拆分前的git checkout
git checkout <commit_id> 切換到特定版本
git checkout master 切換回主線
Fast-forward:
目標分支落後,可直接把指標快轉到最新commit,不會產生新的commit節點。
Three-way merge:
雙方分支都有新提交,同時比較main與feature的差異,最後合併一個新的commit。
git switch main 回到主線
git merge feature/login 合併工作分支
把 feature 合併回 main ,結果就是直接把main指向E,沒有新增commit
A---B---C (main)
\
D---E (feature)
A---B---C (main)
\
D---E (feature)
A---B---C---F (main)
\
D---E (feature)
A---B---C---F------M (main) //M就是新的合併commit節點
\ / //它同時有兩個父節點(E和F)
D------E(feature)
編輯檔案 -> 標記解決 -> 提交
git add .
git commit -m "merge: resolve conflicts between main and feature/login"
git switch feature/login
git fetch origin
git rebase origin/main
逐檔解決後:
git rebase --continue
git branch -d feature/login //合併後刪本地分支
git push origin --delete feature/login //刪遠端分支
git log --oneline --graph --decorate --all